export default {
  HexToRgb: function(str) {
    str = str.replace("#", "");
    const hxs = str.match(/../g);
    if (hxs) {
      const convertedArray = [];
      for (let i = 0; i < 3; i++) {
        const parsedNumber = parseInt(hxs[i], 16);
        if (!isNaN(parsedNumber)) {
          convertedArray.push(parsedNumber);
        }
      }
      return convertedArray;
    }
    return null;
  },
  RgbToHex: function(a, b, c) {
    const hexs = [a.toString(16), b.toString(16), c.toString(16)];
    for (let i = 0; i < 3; i++) {
      if (hexs[i].length === 1) {
        hexs[i] = "0" + hexs[i];
      }
    }
    return "#" + hexs.join("");
  },
  darken: function(color, level) {
    const rgbc = this.HexToRgb(color);
    for (let i = 0; i < 3; i++) {
      rgbc[i] = Math.floor(rgbc[i] * (1 - level));
    }
    return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
  },
  lighten: function(color, level) {
    const rgbc = this.HexToRgb(color);
    for (let i = 0; i < 3; i++) {
      rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i]);
    }
    return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
  }
};
